package com.qf.taobei.controller;

import com.qf.taobei.entity.*;
import com.qf.taobei.entity.bo.CartAdd;
import com.qf.taobei.entity.vo.OrderDetailsVo;
import com.qf.taobei.entity.OrderProduct;
import com.qf.taobei.entity.vo.ProductVo;
import com.qf.taobei.entity.vo.StoreOrderVo;
import com.qf.taobei.entity.vo.ViewProductType;
import com.qf.taobei.mapper.ProductMapper;
import com.qf.taobei.service.OrderService;
import com.qf.taobei.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 吴彦祖
 * @version 1.0
 * @description: TODO
 * @date 2022/2/26 11:27
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductMapper productMapper;

    /**
     *用户根据订单号查询订单
     * @param orderNumber
     * @return
     */

    @GetMapping("/selectOrderByOrderNumber")
    public RespBean selectOrderByOrderNumber(HttpSession session,String orderNumber){
        User user = (User) session.getAttribute("user");
        Integer uid = user.getId();
        List<Order> orders = orderService.selectOrderByOrderNumber(uid,orderNumber);
        if(orders.size() ==0){
            return RespBean.respError("找不到");
        }
        return RespBean.respSuccess("查询成功",orders);
    }

    /**
     * 查询用户所有订单
     * @param session
     * @return
     */
    @GetMapping("/selectOrderListByUserId")
    public RespBean selectOrderListByUserId(HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Order> orders = orderService.selectOrderListByUserId(user.getId());
        if(orders.size() ==0){
            return RespBean.respError("找不到");
        }
        return RespBean.respSuccess("查询成功",orders);
    }

    /**
     * 根据订单号订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/selectOrderDetailsByOrderId")
    public RespBean selectOrderDetailsByOrderId(Integer orderId){
        List<OrderDetailsVo> orderDetailsVoList = orderService.selectOrderDetailsByOrderId(orderId);
        if(orderDetailsVoList.size() == 0){
            return RespBean.respError("查询失败");
        }
        return RespBean.respSuccess("查询成功",orderDetailsVoList);
    }
    /**
     * 根据订单编号订单详情
     * @param orderNumber
     * @return
     */
    @GetMapping("/selectOrderDetailsByOrderNumber")
    public RespBean selectOrderDetailsByOrderNumber(String orderNumber){
        OrderDetailsVo orderDetailsVo= orderService.selectOrderDetailsByOrderNumber(orderNumber);
        if(orderDetailsVo == null){
            return RespBean.respError("查询失败");
        }
        return RespBean.respSuccess("查询成功",orderDetailsVo);
    }
    /**
     * 店家伪删除订单（可接受订单号或者订单编号）
     * @param order
     * @return
     */
    @PostMapping("/stroeDelOrderByOrderId")
    public RespBean stroeDelOrderByOrderId(Order order){
        Integer integer = orderService.stroeDelOrderByOrderId(order);
        if(integer == null){
            return RespBean.respError("删除失败");
        }
        return RespBean.respSuccess("删除成功",integer);
    }

    /**
     * 用户伪删除订单（可接受订单号或者订单编号）
     * @param order
     * @return
     */
    @PostMapping("/userDelOrderByOrderId")
    public RespBean userDelOrderByOrderId(Order order){
        Integer integer = orderService.userDelOrderByOrderId(order);
        if(integer == 0){
            return RespBean.respError("删除失败");
        }
        return RespBean.respSuccess("删除成功",integer);
    }

    /**
     * 用户未提交订单前都可以修改订单的地址
     * 店家也可以通过此接口更新订单状态
     * @param order
     * @return
     */
    @PostMapping("/updeteOrderByUser")
    public RespBean updeteOrderByOrderId(Order order){
        Integer integer = orderService.updeteOrderByOrderId(order);
        if(integer == null){
            return RespBean.respError("更新失败");
        }
        return RespBean.respSuccess("更新成功",integer);
    }
    /**
     * 用户购物车添加订单
     * @param session
     * @return
     */
    @GetMapping("/addOrderCartByUserId")
    public RespBean addOrderByUserId(HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer uid = user.getId();
        List<CartAdd> productList = (List<CartAdd>) session.getAttribute("cartProduct");
        Order order = orderService.addOrderByUserId(uid,productList);
        if(order == null){
            return RespBean.respError("没有对象");
        }
        return RespBean.respSuccess("增加成功",order);
    }
    /**
     * 用户下订单
     * @param session
     * @return
     */
    @GetMapping("/addOrderByUserId")
    public RespBean addOrderByUserId(HttpSession session,Integer productNum){
        User user = (User) session.getAttribute("user");
        Integer uid = user.getId();
        CartAdd cartAdd = new CartAdd();
        List<CartAdd> productList = new ArrayList<>();
        cartAdd.setNumber(productNum);
        cartAdd.setPId((Integer) session.getAttribute("pid"));
        productList.add(cartAdd);
        Order order = orderService.addOrderByUserId(uid,productList);
        if(order == null){
            return RespBean.respError("没有对象");
        }
        return RespBean.respSuccess("增加成功",order);
    }

    /**
     * 添加订单里的商品的评价
     * @param pid
     * @return
     */
    @PostMapping("/addStarToProduct")
    public RespBean addStarToProduct(Integer pid,Integer star){
        Product product = new Product();
        product.setId(pid);
        product.setStar(star);
        RespBean respBean = productService.updateProductById(product);
        return respBean;
    }
    /**
     * 支付订单
     * @param list
     * @return
     */
    @PostMapping("/payToProduct")
    public RespBean payToProduct(List<Integer> list){
        RespBean respBean = new RespBean();
        Product product = new Product();
        for (int i = 0; i < list.size(); i++) {
            product.setId(list.get(i));
            ProductVo productVo = productMapper.queryProductByIdForAfter(list.get(i));
            double sum = productVo.getRepertory()-1;
            product.setProCost(sum);
            respBean = productService.updateProductById(product);
        }
        return respBean;
    }
///**
// * 对单个商品进行评分
// */
//    @PutMapping("/addCommentByProductId")
//    public RespBean addCommentByProductId(Integer pid){
//        Comment comment = orderService.addCommentByProductId(pid);
//        if(comment == null){
//            return RespBean.respError("失败");
//        }
//        return RespBean.respSuccess("增加成功",comment);
//    }
    /**
     * 查询店铺的所有订单
     * @param session
     * @return
     */
    @GetMapping("/selectAllOrderByStoreId")
    public RespBean selectAllOrderByStoreId(HttpSession session){
        Integer storeId = (Integer) session.getAttribute("storeId");
        List<StoreOrderVo> storeOrderVoList = orderService.selectAllOrderByStoreId(storeId);
        if(storeOrderVoList.size() == 0){
            return RespBean.respError("失败");
        }
        return RespBean.respSuccess("成功",storeOrderVoList);
    }

    /**
     * 管理员删除订单（真删除）
     * @param orderNumber
     * @return
     */
    @GetMapping("/delOrder")
    public RespBean delOrder(String orderNumber){
        Integer integer = orderService.delOrder(orderNumber);
        if(integer == 0){
            return RespBean.respError("失败");
        }
        return RespBean.respSuccess("增加成功",integer);
    }

    /**
     * 查询店铺的所有订单
     * @param orderNumber
     * @return
     */
    @GetMapping("/selectOrderNumberLikeByStore")
    public RespBean selectOrderNumberLikeByStore(HttpSession session,String orderNumber){
        Integer storeId = (Integer) session.getAttribute("storeId");
        List<StoreOrderVo> storeOrderVoList = orderService.selectOrderNumberLikeByStore(storeId,orderNumber);
        if(storeOrderVoList.size() == 0){
            return RespBean.respError("失败");
        }
        return RespBean.respSuccess("增加成功",storeOrderVoList);
    }

    /**
     * 店家查询其全部的店的所有订单
     */
    @GetMapping("/selectAllStoreOrderByUserId")
    public RespBean selectAllStoreOrderByUserId(HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer uid = user.getId();
        List<OrderDetailsVo> orderDetailsVoList = orderService.selectAllStoreOrderByUserId(uid);
        if(orderDetailsVoList.size() == 0){
            return RespBean.respError("查询失败");
        }
        return RespBean.respSuccess("查询成功",orderDetailsVoList);
    }

    //根据订单状态查询用户订单
    @GetMapping("/getOrderByStatus")
    public RespBean getOrderByStatus(HttpSession session,Integer status){
        //获取session中的用户
        User user = (User) session.getAttribute("user");
        List<Order> orderList = orderService.getOrderListByStatus(user.getId(),status);
        return RespBean.respSuccess("查询成功",orderList);
    }

    //xjw
    //保存订单ID，给商品评价时使用
    @PostMapping("/saveOrderId")
    public RespBean saveOrderId(HttpSession session,Integer id){
        session.setAttribute("orderId",id);
        return RespBean.respSuccess("保存成功");
    }


    /**
     * author xjw
     * 通过保存在session中的订单id查询商品详情
     * @return
     */
    @GetMapping("/getProductDetails")
    public RespBean getProductDetails(HttpSession session){
        Integer orderId = (Integer) session.getAttribute("orderId");
        List<OrderDetailsVo> orderDetailsVos = orderService.selectOrderDetailsByOrderId(orderId);
        return RespBean.respSuccess("查询成功",orderDetailsVos);
    }

}
